let Camera = require('./camera');
let {vec3, mat4} = require('gl-matrix');
let InputMgr = require('./inputMgr');
let {codes} = require('keycode');
let textureUtils = require('./textureUtils');

let shaderUtils = require('./shaderUtils');
let objParser = require('./objParser');
let GameItem = require('./gameItem');
let ScreenItem = require('./screenItem');
let SimpleGameItem = require('./SimpleGameItem');

const step = 0.4;


let resCache = require('./res/resCache');
let ResItem = require('./res/resItem');
let OrthoCamera = require('./orthoCamera');


//  定义资源path
const baseVSItem = new ResItem('assets/base.vs.glsl');
const baseFSItem = new ResItem('assets/base.fs.glsl');
const ballObjItem = new ResItem('assets/ball.obj');
const planeObjItem = new ResItem('assets/plane.obj');

let y = 0;
let scene = {
    app: null,
    camera: null,
    inputMgr: null,
    items:[],

    ambientColor:vec3.fromValues(0.3,0.3,0.3),
    dirLight:{
       dir:vec3.fromValues(1,1,1),
       color:vec3.fromValues(1,1,1),
        shiness:20,
    },

    loadRes(){
        resCache.addRes(baseVSItem);
        resCache.addRes(baseFSItem);
        resCache.addRes(ballObjItem);
        resCache.addRes(planeObjItem);

        // resCache.addRes(new ResItem([
        //     'assets/skybox/+x.png', 'assets/skybox/-x.png',
        //     'assets/skybox/+y.png', 'assets/skybox/-y.png',
        //     'assets/skybox/+z.png', 'assets/skybox/-z.png',
        // ], ResType.CubeImage));
    },

    init(app) {

        this.app = app;
        let gl = app.gl;
        gl.clearColor(0, 0, 0, 1);

        // init shader
        let baseShader =  shaderUtils.buildShader(baseVSItem.data, baseFSItem.data);
        shaderUtils.createAttribs(baseShader, ['a_position']);
        shaderUtils.createUniforms(baseShader, [
            'projMatrix',
            'mvMatrix',
            'u_color']);

        // init obj
        let cubeData = objParser.parse(ballObjItem.data);

        // 普通 item
        let cubeItem = new SimpleGameItem(baseShader,cubeData);
        mat4.fromTranslation(cubeItem.transMat,vec3.fromValues(0,5,0));
        mat4.fromScaling(cubeItem.scaleMat,vec3.fromValues(3,3,3));
        this.items.push(cubeItem);

        // 创造一个场景
        // 一个地面 + 一个Cube
        // ground
        let groundData = objParser.parse(planeObjItem.data);
        let groundItem = new SimpleGameItem(baseShader,groundData);
        mat4.fromScaling(groundItem.scaleMat,vec3.fromValues(10,10,10));
        groundItem.color[0] = 1.0;
        groundItem.color[1] = 1.0;
        this.items.push(groundItem);

        // init camera
        let pos = vec3.fromValues(-20, 20, 50);
        let target = vec3.fromValues(0, 0, 0);
        let up = vec3.fromValues(0, 1, 0);
        this.camera = new OrthoCamera(pos, target, up);

        // init input
        this.inputMgr = new InputMgr(this);
    },

    update(dt) { // eslint-disable-line
        this.camera.calProjMatrix(this.app.width,this.app.height);
        this.camera.calViewMatrix();

        y++;
        if (y > 360) y-= 360;

        let cube = this.items[0];
        mat4.fromYRotation(cube.rotateMat,y * Math.PI/180);
    },
    render() {
        let gl = globalThis.app.gl;
        let camera = this.camera;

        // 正常绘制
        gl.viewport(0,0,this.app.width,this.app.height);
        gl.clear(gl.DEPTH_BUFFER_BIT | gl.COLOR_BUFFER_BIT);
        this.items.forEach(item=>{
            item.render(camera,this);
        });
    },


    //-------------------------------------------
    // key events
    onKeyDown(event) {
        let camera = this.camera;
        switch (event.keyCode) {
            case codes.s:
                camera.translateZ(-step);
                break;
            case codes.w:
                camera.translateZ(step);
                break;
            case codes.a:
                camera.translateX(step);
                break;
            case codes.d:
                camera.translateX(-step);
                break;
        }
    },
    onKeyUp(event) { // eslint-disable-line
    },

    onMouseDown(x, y) {
        console.log(`onMouseDown x:${x},y:${y}`);
    },
    onMouseUp(x, y) {
        console.log(`onMouseUp x:${x},y:${y}`);
    }
};

module.exports = scene;
